package cn.edu.buaa.cnsatm.traffic_big_data_server.util;

import org.apache.activemq.artemis.api.core.Pair;

import java.util.ArrayList;
import java.util.List;

/**
 * @name = LX
 * @classname = GeometryToPoint2DUtil
 * @description = 将地理信息字段转为Point2D或者其list
 * @date = 2019/10/25
 */
public class GeometryToPoint2DUtil {
    public static void main(String args[]){
        System.out.println(geometryToPoint("POINT(110.59333 17.98889)"));
        System.out.println(geometryToLine("LINESTRING(116.5727778 39.19305556,116.785 39.1625)"));
    }
    public static Point2D geometryToPoint(String geom){
        /**
         * @name = LX
         * @classname = geometryToPoint
         * @description = 将POINT(110.59333 17.98889)转为Point2D
         * @date = 2019/10/25
         */
        geom = geom.substring(6, geom.length() - 1);
        String[] xy = geom.split(" ");
        Point2D p = new Point2D(Double.valueOf(xy[0]), Double.valueOf(xy[1]));
        return p;
    }
    public static Pair<Point2D, Point2D> geometryToLine(String geom){
        /**
         * @name = LX
         * @classname = geometryToLine
         * @description = 将LINESTRING(116.5727778 39.19305556,116.785 39.1625)转为Pair<Point2D, Point2D>
         * @date = 2019/10/25
         */
        geom = geom.substring(11, geom.length() - 1);
        String[] p = geom.split(",");//按逗号分隔，再按点处理
        Point2D p1 = geometryToPoint("POINT(" + p[0] + ")");
        Point2D p2 = geometryToPoint("POINT(" + p[1] + ")");
        Pair<Point2D, Point2D> line = new Pair<>(p1, p2);
        return line;
    }
}
